package com.spring.boot;


import com.spring.util.ClassUtils;

/**
 * @author liyuncong
 * @version 1.0
 * @file WebApplicationType
 * @brief WebApplicationType
 * @details WebApplicationType
 * @date 2023-01-12
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2023-01-12                  liyuncong          Created
 */
public enum WebApplicationType {
    NONE,
    SERVLET,
    REACTIVE;

    private static final String[] SERVLET_INDICATOR_CLASSES = {
        "javax.servlet.Servlet",
        "com.spring.web.context.ConfigurableWebApplicationContext"
    };

    private static final String WEBMVC_INDICATOR_CLASS =
        "com.spring.web.servlet.DispatcherServlet";

    private static final String WEBFLUX_INDICATOR_CLASS =
        "com.spring.web.reactive.DispatcherHandler";

    private static final String JERSEY_INDICATOR_CLASS =
        "org.glassfish.jersey.servlet.ServletContainer";

    static WebApplicationType deduceFromClasspath() {
        if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
            && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
            && !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)
        ) {
            return WebApplicationType.REACTIVE;
        }
        for (String className : SERVLET_INDICATOR_CLASSES) {
            if (!ClassUtils.isPresent(className, null)) {
                return WebApplicationType.NONE;
            }
        }
        return WebApplicationType.SERVLET;
    }
}
